package com.xbongbong.paas.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.MarketingSettingValuePojo;
import com.xbongbong.paas.pojo.vo.MarketingSettingVO;
import com.xbongbong.paas.service.MarketingSettingService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingDetailDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingListDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingSettingSaveDTO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingDetailVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingListVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingSettingSaveVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.MarketingSettingDistributorEntity;
import com.xbongbong.saas.domain.entity.MarketingSettingEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.MarketingManagementSettingEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.MarketingSettingModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("MarketingSettingService")
public class MarketingSettingServiceImpl implements MarketingSettingService {

    @Resource
    private MarketingSettingModel marketingSettingModel;
    @Resource
    private CustomerModel customerModel;


    @Override
    public MarketingSettingListVO getList(MarketingSettingListDTO marketingManagementDTO) throws XbbException {


        MarketingSettingListVO marketingSettingListVO = new MarketingSettingListVO();
        List<MarketingSettingVO> marketingSettingVOList = new ArrayList<>();

        List<MarketingManagementSettingEnum> allEnum = MarketingManagementSettingEnum.getAllEnum();
        for (MarketingManagementSettingEnum managementSettingEnum : allEnum) {
            MarketingSettingVO marketingSettingVO = new MarketingSettingVO();
            marketingSettingVO.setSettingType(managementSettingEnum.getCode());
            marketingSettingVO.setTypeName(managementSettingEnum.getName());
            marketingSettingVOList.add(marketingSettingVO);
        }
        marketingSettingListVO.setSettingList(marketingSettingVOList);
        return marketingSettingListVO;
    }

    @Override
    public MarketingSettingDetailVO getDetail(MarketingSettingDetailDTO marketingManagementDTO) throws XbbException {

        MarketingSettingDetailVO marketingSettingDetailVO = new MarketingSettingDetailVO();
        Integer settingType = marketingManagementDTO.getSettingType();
        String corpid = marketingManagementDTO.getCorpid();
        MarketingSettingEntity marketingSettingEntity = marketingSettingModel.getByType(settingType, corpid);

        if (Objects.nonNull(marketingSettingEntity)) {
            String settingValue = marketingSettingEntity.getSettingValue();
            MarketingSettingValuePojo marketingSettingValuePojo = JSONObject.parseObject(settingValue, MarketingSettingValuePojo.class);
            List<MarketingSettingDistributorEntity> distributorList = marketingSettingValuePojo.getDistributorList();
            Map<Long, String> customerName = new HashMap<>();
            List<Long> customerIds = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(distributorList)) {
                for (MarketingSettingDistributorEntity distributorEntity : distributorList) {
                    JSONArray distributors = distributorEntity.getDistributors();
                    if (CollectionUtils.isNotEmpty(distributors)) {
                        for (int i = 0; i < distributors.size(); i++) {
                            JSONObject item = distributors.getJSONObject(i);
                            Long id = item.getLong(StringConstant.DATA_ID);
                            customerIds.add(id);
                        }
                    }
                }
                List<CustomerEntityExt> customerEntityExts = customerModel.getByKeys(customerIds, corpid);
                customerEntityExts.forEach(item -> customerName.put(item.getId(), item.getData().getString(DistributorEnum.NAME.getAttr())));
                for (MarketingSettingDistributorEntity distributorEntity : distributorList) {
                    JSONArray distributors = distributorEntity.getDistributors();
                    if (CollectionUtils.isNotEmpty(distributors)) {
                        for (int i = 0; i < distributors.size(); i++) {
                            JSONObject item = distributors.getJSONObject(i);
                            Long id = item.getLong(StringConstant.DATA_ID);
                            if (customerName.containsKey(id)) {
                                item.put(StringConstant.SAAS_LINK_BUSINESS_NAME, customerName.get(id));
                            }
                        }
                    }
                }
            }
            marketingSettingDetailVO.setSettingValue(marketingSettingValuePojo);
        }else {
            //没有设置过拿默认配置
            MarketingSettingValuePojo marketingSettingValuePojo = MarketingSettingValuePojo.getByType(settingType);
            marketingSettingDetailVO.setSettingValue(marketingSettingValuePojo);
        }
        return marketingSettingDetailVO;
    }

    @Override
    public MarketingSettingSaveVO save(MarketingSettingSaveDTO marketingManagementDTO) throws XbbException {
        MarketingSettingSaveVO marketingSettingSaveVO = new MarketingSettingSaveVO();
        String corpid = marketingManagementDTO.getCorpid();
        Integer settingType = marketingManagementDTO.getSettingType();
        JSONObject settingValue = marketingManagementDTO.getSettingValue();
        MarketingSettingEntity oldMarketingSettingEntity = marketingSettingModel.getByType(settingType, corpid);
        MarketingSettingEntity marketingSettingEntity = new MarketingSettingEntity();
        marketingSettingEntity.setSettingValue(JSONObject.toJSONString(settingValue));
        marketingSettingEntity.setCorpid(corpid);
        if (Objects.nonNull(oldMarketingSettingEntity)) {
            marketingSettingEntity.setId(oldMarketingSettingEntity.getId());
            marketingSettingModel.update(marketingSettingEntity);
        }else {
            marketingSettingEntity.setSettingType(settingType);
            marketingSettingEntity.setSettingType(settingType);
            marketingSettingModel.insert(marketingSettingEntity);
        }
        return marketingSettingSaveVO;
    }
}
